package com.example.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myblog.dto.BlogMessageDTO;
import com.example.myblog.entity.BlogMessage;
import com.example.myblog.entity.Tag;
import com.example.myblog.exception.BaseException;
import com.example.myblog.mapper.BlogMessageMapper;
import com.example.myblog.result.BaseCodeEnum;
import com.example.myblog.service.BlogMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myblog.service.TagService;
import com.example.myblog.utils.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 博客基本信息 服务实现类
 * </p>
 *
 * @author 叶兴
 * @since 2022-09-29
 */
@Service
public class BlogMessageServiceImpl extends ServiceImpl<BlogMessageMapper, BlogMessage> implements BlogMessageService {

    BlogMessageMapper blogMessageMapper;

    TagService tagService;
    @Autowired
    public void setTagService(TagService tagService) {
        this.tagService = tagService;
    }

    @Autowired
    public void setBlogMessageMapper(BlogMessageMapper blogMessageMapper) {
        this.blogMessageMapper = blogMessageMapper;
    }

    @Override
    public String selectBlogMessageId(String username) {
        QueryWrapper<BlogMessage> blogMapperQueryWrapper = Wrappers.query();
        blogMapperQueryWrapper.eq("user_name",username);
        BlogMessage blogMessage1 = blogMessageMapper.selectOne(blogMapperQueryWrapper);
        return blogMessage1.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBlogMessage(BlogMessage blogMessage) {
        blogMessageMapper.insert(blogMessage);
        return 1;
    }

    @Override
    public boolean ifBlogExists(String blogId) {
        BlogMessage blogMessage = blogMessageMapper.selectById(blogId);
        return blogMessage.getBlogState() == 1;
    }

    @Override
    public int updateBlogMessageLikes(String blogId, String username) throws BaseException {
        UpdateWrapper<BlogMessage> wrapper = new UpdateWrapper<>();
        wrapper.setSql("blog_likes = blog_likes + 1");
        wrapper.eq("user_name", username).eq("id", blogId).eq("blog_state",1);
        int update = blogMessageMapper.update(null, wrapper);
        if (update != 1){
            throw new BaseException(BaseCodeEnum.UPDATE_FAILED);
        }
        return update;
    }

    @Override
    public int updateBlogMessageComments(String blogId, String username) throws BaseException {
        UpdateWrapper<BlogMessage> wrapper = new UpdateWrapper<>();
        wrapper.setSql("blog_comments = blog_comments + 1");
        wrapper.eq("user_name", username).eq("id", blogId).eq("blog_state",1);
        int update = blogMessageMapper.update(null, wrapper);
        if (update != 1){
            throw new BaseException(BaseCodeEnum.UPDATE_FAILED);
        }
        return update;
    }

    @Override
    public List<BlogMessage> getAllBlogs() throws BaseException {
        String userName = UserThreadLocal.getUserDtoName();
        QueryWrapper<BlogMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName).eq("blog_state",1);
        List<BlogMessage> list = list(wrapper);
        if (list == null || list.size() == 0) {
            throw new BaseException(BaseCodeEnum.NO_BLOGS);
        }
        return list;
    }

    @Override
    public Map<String,Object> getBlogs(int page) throws BaseException {
        QueryWrapper<BlogMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_state",1);
        Page<BlogMessage> blogIPage = new Page<>(page,10);
        Page<BlogMessage> blogMessagePage = blogMessageMapper.selectPage(blogIPage, wrapper);
        List<BlogMessage> list = blogMessagePage.getRecords();
        List<BlogMessageDTO> dtos = list.stream().map(blogMessage -> {
            BlogMessageDTO dto = new BlogMessageDTO();
            BeanUtils.copyProperties(blogMessage, dto);
            List<Tag> tags = tagService.getTagsByBlogId(blogMessage.getId());
            dto.setTags(tags);
            return dto;
        }).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>(16);
        map.put("items",dtos);
        map.put("total",blogMessagePage.getTotal());
        return map;
    }

    @Override
    public Map<String,Object> getCategoryBlogs(Integer pageNum,String categoryName) {
        QueryWrapper<BlogMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_state",1).eq("blog_category",categoryName);
        Page<BlogMessage> blogIPage = new Page<>();
        Page<BlogMessage> blogMessagePage = blogMessageMapper.selectPage(blogIPage, wrapper);
        List<BlogMessage> list = blogMessagePage.getRecords();
        List<BlogMessageDTO> dtos = list.stream().map(blogMessage -> {
            BlogMessageDTO dto = new BlogMessageDTO();
            BeanUtils.copyProperties(blogMessage, dto);
            List<Tag> tags = tagService.getTagsByBlogId(blogMessage.getId());
            dto.setTags(tags);
            return dto;
        }).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>(16);
        map.put("items",dtos);
        map.put("total",blogMessagePage.getTotal());
        return map;
    }
}
